package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.message.service.MessageService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.MessageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(description = "消息接口")
@RestController
public class MessageController {

    @Reference
    private MessageService messageService;

    @ApiOperation("学生消息列表")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "token", value = "用户码", required = true, paramType = "header")
    )
    @GetMapping("/messages")
    public PtpResult myMessageList(@ApiIgnore @SsoStudent Student student) {
        MessageResult messageList = messageService.findNotReadMsgByStudentId(student.getId());
        return PtpResultUtils.ok(messageList);
    }

    @ApiOperation("将某个学生的某一类型消息标记为已读")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户码", required = true, paramType = "header"),
            @ApiImplicitParam(name = "type", value = "消息类型（0实习消息，1审批消息，2查岗消息）")
    })
    @GetMapping("/messages/type/{type}/read")
    public PtpResult readMsgByType(@ApiIgnore @SsoStudent Student student,
                                   @PathVariable Integer type) {
        boolean result = messageService.readMessageByStudentIdAndType(student.getId(), type);
        return result ? PtpResultUtils.ok() : PtpResultUtils.error(461, "没有未读消息");
    }
}
